package com.ctgu.train.member.controller;

import com.ctgu.train.common.resp.CommonResp;
import com.ctgu.train.member.req.MemberLoginReq;
import com.ctgu.train.member.req.MemberRegisterReq;
import com.ctgu.train.member.req.MemberSendCodeReq;
import com.ctgu.train.member.resp.MemberLoginResp;
import com.ctgu.train.member.service.MemberService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService  memberService;
    @GetMapping("/count")
    public CommonResp<Integer> count(){
        int count = memberService.count();
        CommonResp<Integer> resp = new CommonResp<>();
        resp.setContent(count);
        return resp;
    }

    @PostMapping("/register")
    public CommonResp<Long> register(@Valid  @RequestBody MemberRegisterReq req){
        long register = memberService.register(req);
//        CommonResp<Long> resp = new CommonResp<>();
//        resp.setContent(register);
//        return resp;
        return new CommonResp<>(register);
    }

    //@RequestBody 用js格式
    @PostMapping("/send-code")
    public CommonResp<Long> sendCode(@Valid  @RequestBody MemberSendCodeReq req){
        memberService.sendCode(req);
        return new CommonResp<>();
    }

    @PostMapping("/login")
    public CommonResp<MemberLoginResp> login(@Valid  @RequestBody MemberLoginReq req){
        MemberLoginResp resp = memberService.login(req);
        return new CommonResp<>(resp);
    }
}
